5. Proximity Analysis

news
code
analysis
Author

kim dayeon

Published

June 14, 2023

Introduction

You are part of a crisis response team, and you want to identify how hospitals have been responding to crash collisions in New York City.


Before you get started, run the code cell below to set everything up.

import math
import pandas as pd
import geopandas as gpd
from geopy.geocoders import Nominatim            # What you'd normally run
#from learntools.geospatial.tools import Nominatim # Just for this exercise

import folium 
from folium import Marker
from folium.plugins import MarkerCluster

from folium.plugins import HeatMap
from folium import Marker, GeoJson

import pandas as pd
import geopandas as gpd
def embed_map(m, file_name):
    from IPython.display import IFrame
    m.save(file_name)
    return IFrame(file_name, width='100%', height='500px')

Exercises

1) Visualize the collision data.

Run the code cell below to load a GeoDataFrame collisions tracking major motor vehicle collisions in 2013-2018.

collisions = gpd.read_file("D:/archive (1)/NYPD_Motor_Vehicle_Collisions/NYPD_Motor_Vehicle_Collisions/NYPD_Motor_Vehicle_Collisions.shp")
collisions.head()
DATE TIME BOROUGH ZIP CODE LATITUDE LONGITUDE LOCATION ON STREET CROSS STRE OFF STREET ... CONTRIBU_2 CONTRIBU_3 CONTRIBU_4 UNIQUE KEY VEHICLE TY VEHICLE _1 VEHICLE _2 VEHICLE _3 VEHICLE _4 geometry
0 07/30/2019 0:00 BRONX 10464 40.841100 -73.784960 (40.8411, -73.78496) NaN NaN 121 PILOT STREET ... Unspecified NaN NaN 4180045 Sedan Station Wagon/Sport Utility Vehicle Station Wagon/Sport Utility Vehicle NaN NaN POINT (1043750.211 245785.815)
1 07/30/2019 0:10 QUEENS 11423 40.710827 -73.770660 (40.710827, -73.77066) JAMAICA AVENUE 188 STREET NaN ... NaN NaN NaN 4180007 Sedan Sedan NaN NaN NaN POINT (1047831.185 198333.171)
2 07/30/2019 0:25 NaN NaN 40.880318 -73.841286 (40.880318, -73.841286) BOSTON ROAD NaN NaN ... NaN NaN NaN 4179575 Sedan Station Wagon/Sport Utility Vehicle NaN NaN NaN POINT (1028139.293 260041.178)
3 07/30/2019 0:35 MANHATTAN 10036 40.756744 -73.984590 (40.756744, -73.98459) NaN NaN 155 WEST 44 STREET ... NaN NaN NaN 4179544 Box Truck Station Wagon/Sport Utility Vehicle NaN NaN NaN POINT (988519.261 214979.320)
4 07/30/2019 10:00 BROOKLYN 11223 40.600090 -73.965910 (40.60009, -73.96591) AVENUE T OCEAN PARKWAY NaN ... NaN NaN NaN 4180660 Station Wagon/Sport Utility Vehicle Bike NaN NaN NaN POINT (993716.669 157907.212)

5 rows × 30 columns

“LATITUDE” 및 “LONGITUDE” 열을 사용하여 충돌 데이터를 시각화하는 대화형 지도를 만듭니다. 어떤 유형의 지도가 가장 효과적이라고 생각하십니까?

m_1 = folium.Map(location=[40.7, -74], zoom_start=11) 

HeatMap(data=collisions[['LATITUDE', 'LONGITUDE']], radius=15).add_to(m_1)

m_1
Make this Notebook Trusted to load map: File -> Trust Notebook

2) Understand hospital coverage.

Run the next code cell to load the hospital data.

hospitals = gpd.read_file("D:/archive (1)/nyu_2451_34494/nyu_2451_34494/nyu_2451_34494.shp")
hospitals.head()
id name address zip factype facname capacity capname bcode xcoord ycoord latitude longitude geometry
0 317000001H1178 BRONX-LEBANON HOSPITAL CENTER - CONCOURSE DIVI... 1650 Grand Concourse 10457 3102 Hospital 415 Beds 36005 1008872.0 246596.0 40.843490 -73.911010 POINT (1008872.000 246596.000)
1 317000001H1164 BRONX-LEBANON HOSPITAL CENTER - FULTON DIVISION 1276 Fulton Ave 10456 3102 Hospital 164 Beds 36005 1011044.0 242204.0 40.831429 -73.903178 POINT (1011044.000 242204.000)
2 317000011H1175 CALVARY HOSPITAL INC 1740-70 Eastchester Rd 10461 3102 Hospital 225 Beds 36005 1027505.0 248287.0 40.848060 -73.843656 POINT (1027505.000 248287.000)
3 317000002H1165 JACOBI MEDICAL CENTER 1400 Pelham Pkwy 10461 3102 Hospital 457 Beds 36005 1027042.0 251065.0 40.855687 -73.845311 POINT (1027042.000 251065.000)
4 317000008H1172 LINCOLN MEDICAL & MENTAL HEALTH CENTER 234 E 149 St 10451 3102 Hospital 362 Beds 36005 1005154.0 236853.0 40.816758 -73.924478 POINT (1005154.000 236853.000)

“위도” 및 “경도” 열을 사용하여 병원 위치를 시각화합니다.

m_2 = folium.Map(location=[40.7, -74], zoom_start=11) 

for idx, row in hospitals.iterrows():
    Marker([row['latitude'], row['longitude']]).add_to(m_2)


m_2
Make this Notebook Trusted to load map: File -> Trust Notebook

3) 가장 가까운 병원이 10km 이상 떨어진 때는 언제였습니까?

가장 가까운 병원에서 10km 이상 떨어진 곳에서 충돌이 발생한 ’collisions’의 모든 행을 포함하는 DataFrame ’outside_range’를 만듭니다.

‘병원’과 ’충돌’ 모두 좌표 기준 시스템으로 EPSG 2263을 사용하고 EPSG 2263의 단위는 미터입니다.

coverage = gpd.GeoDataFrame(geometry=hospitals.geometry).buffer(10000)
my_union = coverage.geometry.unary_union
outside_range = collisions.loc[~collisions["geometry"].apply(lambda x: my_union.contains(x))]

다음 코드 셀은 가장 가까운 병원에서 10km 이상 떨어진 곳에서 발생한 충돌 비율을 계산합니다.

percentage = round(100*len(outside_range)/len(collisions), 2)
print("Percentage of collisions more than 10 km away from the closest hospital: {}%".format(percentage))
Percentage of collisions more than 10 km away from the closest hospital: 15.12%

4) Make a recommender.

먼 곳에서 충돌이 발생하면 부상자를 가장 가까운 병원으로 이송하는 것이 더욱 중요해집니다.

이를 염두에 두고 다음과 같은 추천자를 만들기로 결정합니다. - 충돌 위치(EPSG 2263에서)를 입력으로 사용합니다. - 가장 가까운 병원(EPSG 2263에서 거리 계산이 수행되는 곳)을 찾고, - 가장 가까운 병원의 이름을 반환합니다.

def best_hospital(collision_location):
    name = value_hospital
    return name

# Test your function: this should suggest CALVARY HOSPITAL INC
print(best_hospital(outside_range.geometry.iloc[0]))
NameError: name 'value_hospital' is not defined

5) 수요가 가장 많은 병원은?

‘outside_range’ DataFrame의 충돌만 고려했을 때 가장 추천하는 병원은?

응답은 4)에서 생성한 함수에서 반환한 병원 이름과 정확히 일치하는 Python 문자열이어야 합니다.

6) Where should the city construct new hospitals?

Run the next code cell (without changes) to visualize hospital locations, in addition to collisions that occurred more than 10 kilometers away from the closest hospital.

m_6 = folium.Map(location=[40.7, -74], zoom_start=11) 

coverage = gpd.GeoDataFrame(geometry=hospitals.geometry).buffer(10000)
folium.GeoJson(coverage.geometry.to_crs(epsg=4326)).add_to(m_6)
HeatMap(data=outside_range[['LATITUDE', 'LONGITUDE']], radius=9).add_to(m_6)
folium.LatLngPopup().add_to(m_6)

embed_map(m_6, 'm_6.html')